package com.huanwei.login_regist;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huanwei.cc.R;
import com.huanwei.cc.bean.KeBiao;
import com.huanwei.cc.bean.Teachet;
import com.huanwei.cc.newbean.KeCheng;
import com.huanwei.cc.newbean.LoginT;
import com.huanwei.cc.utils.ALL_CONFIG;
import com.huanwei.cc.utils.JSONUtils;
import com.huanwei.cc.utils.LocationUtils;
import com.huanwei.http.API;
import com.huanwei.http.Okhttp;
import com.jaredrummler.materialspinner.MaterialSpinner;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我要签到页面
 */
public class MyQianDao_Activity extends AppCompatActivity {
    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.spinner_shijianduan)
    MaterialSpinner spinnerShijianduan;

    @BindView(R.id.but_regist)
    Button butRegist;
    @BindView(R.id.tv_xingqi)
    TextView tvXingqi;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_laoshi)
    TextView tvLaoshi;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myqiandao);
        ButterKnife.bind(this);
        initview();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getCurriculums();
    }

    String kecheng;
    List<KeBiao.DataBean> ccList;
    String week;
    String Shijianduan;
    String Address;
    String Teacher_name;

    private void initview() {


        spinnerShijianduan.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                kecheng = teachers.get(position);
//                week = ccList.get(position).getWeek();
//                Shijianduan = ccList.get(position).getShijianduan();
//                Address = ccList.get(position).getAddress();
//                Teacher_name = ccList.get(position).getTeacher_name();
                tvXingqi.setText(week);
                tvTime.setText(Shijianduan);
                tvAddress.setText(Address);
                tvLaoshi.setText(Teacher_name);

            }
        });

    }

    @OnClick({R.id.img_back, R.id.but_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.but_regist:

//                String shangkeName = edName.getText().toString();
//                if (TextUtils.isEmpty(shangkeName)) {
//                    Toast.makeText(this, "请输入上课地址", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                String Kechengname = editKechengname.getText().toString();
//                if (TextUtils.isEmpty(Kechengname)) {
//                    Toast.makeText(this, "请输入课程名称", Toast.LENGTH_SHORT).show();
//                    return;
//                }


//                API.addmyCurriculum(kecheng, Teacher_name, week, Shijianduan, Address, "", "", new Okhttp.Objectcallback() {
//                    @Override
//                    public void onsuccess(String st) {
//                        Log.e("111", "xx" + st);
//
//
//                        LoginT login = JSONUtils.parseJSON(st, LoginT.class);
//                        if (login.isIssuccess() == true) {
//                            Toast.makeText(MyQianDao_Activity.this, "添加课程成功！", Toast.LENGTH_SHORT).show();
//
////                            finish();
//                        } else {
//                            Toast.makeText(MyQianDao_Activity.this, login.getMsg(), Toast.LENGTH_SHORT).show();
//
//                        }
//                    }
//
//                    @Override
//                    public void onFalia(int code, String errst) {
//                        Toast.makeText(MyQianDao_Activity.this, "网络错误！", Toast.LENGTH_SHORT).show();
//                    }
//                });
                qiandao();

                break;
        }
    }

    List<String> teachers = new ArrayList<String>();

    public void getCurriculums() {
        API.getmyCurriculums(new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("111", "xx" + st);


                KeBiao teacher = JSONUtils.parseJSON(st, KeBiao.class);
                if (teacher.isIssuccess() == true) {

                    ccList = teacher.getData();

//                    for (int i = 0; i < ccList.size(); i++) {
//                        teachers.add(ccList.get(i).getSubject_name());
//                    }
//                    kecheng = teachers.get(0);
//                    week = ccList.get(0).getWeek();
//                    Shijianduan = ccList.get(0).getShijianduan();
//                    Address = ccList.get(0).getAddress();
//                    Teacher_name = ccList.get(0).getTeacher_name();
                    tvXingqi.setText(week);
                    tvTime.setText(Shijianduan);
                    tvAddress.setText(Address);
                    tvLaoshi.setText(Teacher_name);
                    spinnerShijianduan.setItems(teachers);

                } else {
                    Toast.makeText(MyQianDao_Activity.this, teacher.getMsg(), Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFalia(int code, String errst) {
                Toast.makeText(MyQianDao_Activity.this, "网络错误！", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void qiandao() {
        API.getTeachermessage(Teacher_name, new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("111", "xx" + st);
                Teachet teachet = JSONUtils.parseJSON(st, Teachet.class);
                String isshangban = teachet.getData().getIsshangban();
                String address = teachet.getData().getAddress();



                if (isshangban!=null&&isshangban.equals("上班")) {
                    String[] strs = address.split(",");
                    double distance = LocationUtils.getDistance(Double.valueOf(strs[0]), Double.valueOf(strs[1]),
                            Double.valueOf(ALL_CONFIG.Latitude), Double.valueOf(ALL_CONFIG.Longitude));
                    double v = distance / 1000 / 1000;
                    if (v > 20) {
                        Toast.makeText(MyQianDao_Activity.this, "距离老师太远，不能签到", Toast.LENGTH_SHORT).show();
                    } else {
                        xueshengqiandao();
                    }
//                    System.out.println("距离" + distance / 1000 + "公里");


                } else {
                    Toast.makeText(MyQianDao_Activity.this, "老师未上班，不能签到", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFalia(int code, String errst) {
                Log.e("111", "xx" + errst);

                Toast.makeText(MyQianDao_Activity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void xueshengqiandao() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        API.StudentSign(Teacher_name,
                "", kecheng, df.format(new Date()),
                Shijianduan, new Okhttp.Objectcallback() {
                    @Override
                    public void onsuccess(String st) {
                        Log.e("111111", "xx" + st);
                        Teachet teachet = JSONUtils.parseJSON(st, Teachet.class);
                        boolean issuccess = teachet.isIssuccess();
                        if (issuccess) {
                            Toast.makeText(MyQianDao_Activity.this, "签到成功", Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(MyQianDao_Activity.this, teachet.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFalia(int code, String errst) {
                        Toast.makeText(MyQianDao_Activity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                }
        );
    }
}
